Skip to content

Ensured that no fields are returned when sparse fields is set to an empty value #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024

Conversation

sliverc
Copy link
Member

@sliverc sliverc commented Jun 27, 2024

Fixes #1238

Description of the Change

When a sparse field is set to an empty value no fields (attributes/relationships) should be rendered whereas currently it rendered all fields.

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name in AUTHORS

@sliverc sliverc requested a review from n2ygk June 27, 2024 19:35
Copy link
Contributor

@n2ygk n2ygk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current behavior is supported by the spec:


If a client does not specify the set of fields for a given resource type, the server MAY send all fields, a subset of fields, or no fields for that resource type.

https://jsonapi.org/format/
8.4 Sparse Fieldsets

It could break a lot of implementations. Perhaps add a setting.

@sliverc
Copy link
Member Author

sliverc commented Jun 28, 2024

I agree that the sentence you mentioned is a bit ambiguous, however it would not make any sense to first specify that The value of any fields[TYPE] parameter MUST and then afterward say that the server MAY. So the sentence you mention is, as far as I see it, if there is no query parameter set at all for a given resource type, but not if it is explicitly set to an empty value.

In any case, I have double-checked what was the behavior before change #1221. It turned out when defining an empty value it would also return no fields. So this means it is a regression and this PR will revert to the behavior we had before version 7.0.0. I have clarified this in the changelog as well now.

@n2ygk n2ygk merged commit d1163ce into django-json-api:main Jun 28, 2024
11 checks passed
@sliverc sliverc deleted the sparse_fields_empty_value branch June 28, 2024 14:04
@sliverc
Copy link
Member Author

sliverc commented Jun 28, 2024

Thanks @n2ygk for merging. Let me prepare a release to get those regressions out of the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sparse field set should not return all fields when value is empty
2 participants